﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using NSR.HD.Common;
using NSR.LocalService.Test;
using NSR.Model;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “基本页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234237 上有介绍

namespace NSR.HD
{
    /// <summary>
    /// 基本页，提供大多数应用程序通用的特性。
    /// </summary>
    public sealed partial class Index : NSR.HD.Common.LayoutAwarePage
    {
        /// <summary>
        /// 推荐榜长度
        /// </summary>
        private const int SizeRanklist = 8;

        private List<ArticleInfo> _rankList;

        /// <summary>
        /// 全本榜长度
        /// </summary>
        private const int SizeCompletelylist = 6;

        private List<ArticleInfo> _completelylist;


        public Index()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="navigationParameter">最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
        /// </param>
        /// <param name="pageState">此页在以前会话期间保留的状态
        /// 字典。首次访问页面时为 null。</param>
        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            //1.读取本地List
            //2.如果为空则从服务端获取List
            //3.挂起时list序列化到本地
            // Restore values stored in app data.
            //Windows.Storage.ApplicationDataContainer localSettings =
            //    Windows.Storage.ApplicationData.Current.LocalSettings;
            //if (localSettings.Values.ContainsKey("RankList"))
            //    _rankList = localSettings.Values["RankList"] as List<ArticleInfo>;

            //if (_rankList == null)
            //    _rankList = NSR.Service.ArticleService.GetRankList(SizeRanklist);

            //if (_rankList != null)
            //    lvRankList.DataContext = _rankList;

            //if (localSettings.Values.ContainsKey("Completelylist"))
            //    _completelylist = localSettings.Values["Completelylist"] as List<ArticleInfo>;

            //if (_completelylist == null)
            //    _completelylist = NSR.Service.ArticleService.GetCompletelyList(SizeCompletelylist);

            //if (_completelylist != null)
            //    lvCompletelylist.DataContext = _completelylist;

            //List<ArticleInfo> list = new List<ArticleInfo>();

            //list.Add(new ArticleInfo { ArticleID = 1, ArticleName = "Lee", Author = "Jetson" });
            //list.Add(new ArticleInfo { ArticleID = 2, ArticleName = "Chen", Author = "Mc" });
            //list.Add(new ArticleInfo { ArticleID = 3, ArticleName = "He", Author = "James" });

            //DataContractSerializer ser = new DataContractSerializer(typeof(List<ArticleInfo>));
            //MemoryStream ms = new MemoryStream();
            //ser.WriteObject(ms, list);
            //// 获取 SessionState 文件的输出流并以异步方式写入状态
            //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("list.xml", CreationCollisionOption.ReplaceExisting);
            //using (Stream fileStream = await file.OpenStreamForWriteAsync())
            //{
            //    ms.Seek(0, SeekOrigin.Begin);
            //    await ms.CopyToAsync(fileStream);
            //    await fileStream.FlushAsync();
            //}
            Person person = new Person();
            person.Create();
            person.Insert();
            var list = person.Query();
        }

        /// <summary>
        /// 保留与此页关联的状态，以防挂起应用程序或
        /// 从导航缓存中放弃此页。值必须符合
        /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。
        /// </summary>
        /// <param name="pageState">要使用可序列化状态填充的空字典。</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            if (_rankList != null)
            {
                //pageState["RankList"] = _rankList[0];

            }
        }
    }

   
}
